package com.xinguan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinguan.entity.Commodity;
import com.xinguan.entity.Types;
import com.xinguan.mapper.CommodityMapper;
import com.xinguan.mapper.TypesMapper;
import com.xinguan.pojo.dto.CommodityDto;
import com.xinguan.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 陈俊行
 * @Date: 2023/11/05/9:39
 * @Description:
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {
    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private TypesMapper typesMapper;
    @Override
    public void increaseCommodity(CommodityDto commodityDto) {
            //Commodity commodity1= new Commodity();
            //BeanUtil.copyProperties(commodity1,commodityDto);
            Commodity commodity1= new Commodity();
            commodity1.setCommodityName(commodityDto.getCommodityName());
            commodity1.setCost(commodityDto.getCost());
            commodity1.setCurrentPrice(commodityDto.getCurrentPrice());
            commodity1.setInventory(commodityDto.getInventory());
            commodity1.setDangerInventory(commodityDto.getDangerInventory());
            commodity1.setSellingUnit(commodityDto.getSellingUnit());
            commodity1.setInventoryStatus((commodityDto.getInventory()>=commodityDto.getDangerInventory())?1:0);
            commodity1.setFile(commodityDto.getFile());
            commodity1.setCategoryId(commodityDto.getCategoryId());
            LambdaQueryWrapper<Types> wrapper2= new LambdaQueryWrapper<>();
            wrapper2.eq(Types::getCategoryId,commodityDto.getCategoryId());
            Types types = typesMapper.selectOne(wrapper2);
            Integer curToal = types.getCurToal();
            curToal+=1;
            types.setCurToal(curToal);
            typesMapper.updateById(types);
            commodityMapper.insert(commodity1);

    }


    @Override
    public PageInfo<Commodity> getCommodities(String id, String commodityName, Integer inventoryStatus, Integer popularity, Integer count, Integer pageSize) {
        PageHelper.startPage(count,pageSize);
        List<Commodity> commodities = commodityMapper.selectByParam(id, commodityName, inventoryStatus, popularity);
        PageInfo<Commodity> page = new PageInfo<>(commodities);
         return page;
    }

    @Override
    public void updateCommodity(Commodity commodity) {
       LambdaQueryWrapper<Commodity> wrapper =new LambdaQueryWrapper<>();
       wrapper.eq(Commodity::getId,commodity.getId());
        Commodity commodity1 = commodityMapper.selectOne(wrapper);
        commodity1.setCost(commodity.getCost());
        commodity1.setCurrentPrice(commodity.getCurrentPrice());
        commodity1.setInventory(commodity.getInventory());
        commodity1.setDangerInventory(commodity.getDangerInventory());
        commodity1.setSellingUnit(commodity.getSellingUnit());
        commodityMapper.updateById(commodity1);
    }
}
